---
--- i26_flower_sack.lua
--- Description: 背包
--- Author: 旅行者
--- Date: 2025/3/1 11:32
---
local I26_FLOWER_SACK = TUNING.ENTITY_LIST.I26_FLOWER_SACK
local assets =
{
    Asset("IMAGE", I26_FLOWER_SACK.imagePath), --物品栏贴图
    Asset("ATLAS", I26_FLOWER_SACK.atlasPath), --物品栏贴图
    Asset("ANIM", I26_FLOWER_SACK.anim), -- 地上的贴图
    Asset("ANIM", I26_FLOWER_SACK.swapAnim), -- 背包动画
    Asset("ANIM", "anim/i26_ui_bigbag_3x12.zip"),
    Asset("ANIM", "anim/i26_ui_bigbag_3x5.zip"),
    Asset("ANIM", "anim/i26_ui_bigbag_3x8.zip"),
    Asset("ANIM", "anim/i26_new_ui_bigbag_3x12.zip"),
    Asset("ANIM", "anim/i26_new_ui_bigbag_3x8.zip"),
    Asset("ANIM", "anim/i26_new_ui_bigbag_3x5.zip"),
    Asset( "IMAGE", "images/inventoryimages/spore_slot.tex" ),
    Asset( "ATLAS", "images/inventoryimages/spore_slot.xml" ),
}
-- 穿上装备
local function onequip_flower(inst, owner)
    -- 覆盖玩家的动画符号，使其显示为插件的外观
    owner.AnimState:OverrideSymbol("swap_body", I26_FLOWER_SACK.swapScmlName, "swap_body")
    --owner.AnimState:OverrideSymbol("backpack", "ui_bigbag_3x12", "backpack")
    inst.components.container:Open(owner)
    if inst.components.i26_flower_sack_data then
        -- 检查组件，触发对应的效果
        inst.components.i26_flower_sack_data:EquipFn(inst)
    end
end

local function onunequip_flower(inst, owner)
    -- 清除玩家的动画符号覆盖
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
    inst.components.container:Close(owner)
    if inst.components.i26_flower_sack_data then
        -- 检查组件，触发对应的效果
        inst.components.i26_flower_sack_data:UnEquipFn(inst)
    end
end

-- 定义物品被丢弃时的行为
local function ondropped(inst)
    if inst.components.container ~= nil then
        inst.components.container:Close()
    end
    if inst.components.i26_flower_sack_data then
        inst.components.i26_flower_sack_data:UnEquipFn(inst)
    end
    inst.AnimState:SetBank(I26_FLOWER_SACK.id)
    inst.AnimState:SetBuild(I26_FLOWER_SACK.scmlName)
    inst.AnimState:PlayAnimation("idle")
end

-- 定义装备到模型时的行为（通常用于展示）
local function onequiptomodel(inst, owner)
    -- 关闭插件的容器界面
    inst.components.container:Close(owner)
end

-- 检查是否可以接收的物品
local function AcceptTest_tt(inst, item, giver)
    if TUNING.I26_FLOWER_SACK_ENABLED ~= "enabled" then return false end
    if inst.components.i26_flower_sack_data then
        return inst.components.i26_flower_sack_data:CanAcceptItem(inst, item, giver)
    end
    return false
end

-- 接受物品时更新特效进度, 并移除物品
local function OnAccept_tt(inst, giver, item)
    if inst.components.i26_flower_sack_data then
        inst.components.i26_flower_sack_data:UpdateData(inst, giver, item)
    end
end

local function fn_flower()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    -- 设置物理属性，使其可以被拾取
    MakeInventoryPhysics(inst)

    inst:AddTag("backpack")
    inst:AddTag("waterproofer")
    inst:AddTag("i26_flower_sack")
    inst:AddTag("shoreonsink")  --不掉深渊
    inst:AddTag("tornado_nosucky") --mod兼容：永不妥协。不会被龙卷风刮走
    inst:AddTag("hide_percentage") -- 隐藏百分比
    inst:AddTag("fridge") -- 保鲜
    inst:AddTag("nosteal") -- 防偷取

    inst.AnimState:SetBank(I26_FLOWER_SACK.id)
    inst.AnimState:SetBuild(I26_FLOWER_SACK.scmlName)
    inst.AnimState:PlayAnimation("idle")

    inst.entity:SetPristine()

    --if not TheWorld.ismastersim then
    --    inst.OnEntityReplicated = function(inst)
    --        inst.replica.container:WidgetSetup(I26_FLOWER_SACK.id)
    --    end
    --    return inst
    --end
    -- 设置浮动属性
    MakeInventoryFloatable(inst)

    if not TheWorld.ismastersim then
        return inst
    end

    -- 添加 i26_flower_sack_data 组件
    inst:AddComponent("i26_flower_sack_data")

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.cangoincontainer = false
    inst.components.inventoryitem.atlasname = I26_FLOWER_SACK.atlasPath
    inst.components.inventoryitem:SetOnDroppedFn(ondropped)

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BACK or EQUIPSLOTS.BODY
    inst.components.equippable:SetOnEquip(onequip_flower)
    inst.components.equippable:SetOnUnequip(onunequip_flower)
    inst.components.equippable:SetOnEquipToModel(onequiptomodel)

    --inst:AddComponent("waterproofer") -- 防水
    --inst.components.waterproofer:SetEffectiveness(.0)

    --inst:AddComponent("armor") -- 护甲
    --inst.components.armor:InitIndestructible(.0)

    --inst:AddComponent("planardefense") -- 位面防御
    --inst.components.planardefense:SetBaseDefense(0)

    inst:AddComponent("container")
    inst.components.container:WidgetSetup(I26_FLOWER_SACK.id)

    -- 交易组件
    --inst:AddComponent("trader")
    --inst.components.trader.deleteitemonaccept = false
    --inst.components.trader.acceptnontradable = true
    --inst.components.trader:SetAcceptTest(AcceptTest_tt)
    --inst.components.trader.onaccept = OnAccept_tt

    MakeHauntableLaunchAndDropFirstItem(inst)

    return inst
end

return Prefab(I26_FLOWER_SACK.id, fn_flower, assets)